package com.soul.common.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.stereotype.Component;

@Component
public class CustomIdGenerator implements IdentifierGenerator {
    // 保留15位
    private final int size = 11;
    @Override
    public Long nextId(Object entity) {
        Snowflake snowflake = IdUtil.getSnowflake();
        long id = snowflake.nextId();
        String idStr = String.valueOf(id);
        int length = idStr.length();
        if (length > size){
            String substring = idStr.substring(length - size - 1, length - 1);
            id = Long.parseLong("1".concat(substring));
        }
        //返回生成的id值即可.
        return id;
    }
}
